package com.acme.bankapp.tcp.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.acme.CommandHandler;

/**
 * Client accepts the following commands which are typed as Strings in console:<br>
 * 1) bye - to cancel connection 2) add>data_feed_to_the_bank - adds new client
 * to the bank. <br>
 * Example: add>accounttype=c|s;balance=100;overdraft=50;name=John;gender=m|f;email=john@email.com;
 */
public class BankClient {

    private static final String ERROR_DATA_FORMAT = "data received in unknown format";
    private static final String ERROR_CONNECTION_HOST = "You are trying to connect to an unknown host!";

	private Socket requestSocket;
	private ObjectOutputStream out;
	private ObjectInputStream in;
	private String clientCommand;
	private String serverMessage;

	public BankClient() {
	}

	void run() {
		try {
			// 1. creating a socket to connect to the server
			requestSocket = new Socket("localhost", 2004);
			System.out.println("client-log> Connected to localhost in port 2004");

			// 2. get Input and Output streams
			out = new ObjectOutputStream(requestSocket.getOutputStream());

			in = new ObjectInputStream(requestSocket.getInputStream());

			// 3: Communicating with the server
            do {
                try {
                    // get and print message from the server
                    serverMessage = (String) in.readObject();
                    System.out.println("server-to-client> " + serverMessage);

                    // get input from the user console
                    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
                    clientCommand = inFromUser.readLine();

                    // send user message to the server
                    sendMessage(clientCommand);
                } catch (ClassNotFoundException classNot) {
                    System.err.println(ERROR_DATA_FORMAT);
                }
            } while (!clientCommand.equals(CommandHandler.CLOSE_CONNECTION));
		} catch (UnknownHostException unknownHost) {
			System.err.println(ERROR_CONNECTION_HOST);
		} catch (IOException ioException) {
			ioException.printStackTrace();
		} finally {
			// 4: Closing connection
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
                if (requestSocket != null) {
                    requestSocket.close();
                }
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
		}
	}

	private void sendMessage(final String msg) {
		try {
			out.writeObject(msg);
			out.flush();
			System.out.println("client-to-server> " + msg);
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}

	public static void main(final String args[]) {
		BankClient client = new BankClient();
		client.run();
	}

}
